package cn.tedu.hm.service.impl;

import cn.tedu.hm.ex.ServiceException;
import cn.tedu.hm.mapper.ServiceMapper;
import cn.tedu.hm.pojo.dto.ServiceAddNewDTO;
import cn.tedu.hm.pojo.vo.OrderListItemVO;
import cn.tedu.hm.pojo.vo.RoomStandardVO;
import cn.tedu.hm.pojo.vo.ServiceListItemVO;
import cn.tedu.hm.pojo.vo.ServiceStandardVO;
import cn.tedu.hm.service.IServiceService;
import cn.tedu.hm.wep.JsonPage;
import cn.tedu.hm.wep.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * tj41h
 */
@Slf4j
@Service
public class ServiceServiceImpl implements IServiceService {

    @Autowired
    private ServiceMapper serviceMapper;

    @Override
    public void add(ServiceAddNewDTO serviceAddNewDTO) {
        log.debug("开始处理新增服务的业务，参数：{}", serviceAddNewDTO);
        int rows = serviceMapper.insert(serviceAddNewDTO);
        if (rows !=1) {
            // 是：抛出异常
            String message = "新增服务失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public JsonPage<ServiceListItemVO> list(Integer page, Integer pageSize) {
        log.debug("开始处理【服务列表】的业务，无参数");
        PageHelper.startPage(page,pageSize);
        List<ServiceListItemVO> list = serviceMapper.list();
        if (list == null) {
            // 是：抛出异常
            String message = "数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public JsonPage<ServiceListItemVO> getListByName(String name, Integer page, Integer pageSize) {
        log.debug("开始处理查询服务的处理，当前页：{},条数：{}",page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<ServiceListItemVO> queryResult = serviceMapper.getListByName(name);
        log.debug("查询结果为{}",queryResult);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "根据名字模糊查询服务列表失败，服务数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return JsonPage.restPage(new PageInfo<>(queryResult));

    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除服务的业务");
        ServiceStandardVO queryResult = serviceMapper.getStandardById(id);
        log.debug("根据ID={}检查服务数据是否存在，查询结果：{}", id, queryResult);
        if (queryResult == null) {
            String message = "删除失败,服务数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        int rows = serviceMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除失败,服务器忙,请稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }
}
